<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\Detail;
use App\Admin\Repositories\Feedback;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;

class FeedbackController extends AdminController
{

    public bool $create = false;
    public bool $edit = false;
    public string $edit_permission_name = 'feedback-edit';
    public string $delete_permission_name = 'feedback-delete';
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(new Feedback(), function (Grid $grid) {
            $grid->model()->with('user')->orderByDesc('id');
            $grid->column('id');
            $grid->column('user.phone', trans('user.fields.phone'));
            $grid->column('images')
                ->display(function () {
                    return $this->getImages();
                })->image('', 50, 50);
            $grid->column('content');
            if (Admin::user()->can($this->edit_permission_name) && !$this->isTrashed()) {
                $grid->column('status')->switch();
            } else {
                $grid->column('status')->bool();
            }
            $grid->column('created_at');


            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append(new Detail());
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('status')->select(['0' => '未处理', '1' => '已处理']);

            });
//            $grid->disableCreateButton();
//            $grid->disableViewButton();
//            $grid->disableEditButton();
            $this->gridCommon($grid);
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    public function detail(int $id): Show
    {
        return Show::make($id, new Feedback(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('images')->as(function () {
                return $this->getImages();
            })->image('', 50, 50);
            $show->field('content');
            $show->field('status')->bool();
            $show->field('created_at');
            $show->field('updated_at');
        });
    }


    protected function form(): Form
    {
        return Form::make(new Feedback(), function (Form $form) {
            $form->text('status');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }

}
